package com.kkb.controller;

import com.github.pagehelper.PageInfo;
import com.kkb.pojo.Role;
import com.kkb.service.RoleService;
import com.kkb.vo.ResultVO;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("role")
@ResponseBody
public class RoleController {

    @Resource
    private RoleService roleService;

    @RequestMapping(value = "list",method = RequestMethod.GET)
    public ResultVO<Role> selectRoleInPage(Integer index, Integer size, String name){
        System.out.println(name);
        PageInfo<Role> rolePageInfo =roleService.selectRoleInPage(index, size, name);
        if (rolePageInfo!=null){
            return new ResultVO<>(rolePageInfo);
        }
        return new ResultVO<>(404,"暂无角色");
    }

    @RequestMapping(value = "",method = RequestMethod.POST)
    public ResultVO<Role> addRole(Role role,
                                  @RequestParam(value = "list", required = false) List<com.kkb.pojo.Resource> list,
                                  @RequestParam("list1") List<Integer> list1){
        if (roleService.insertRole(role, list1)){
            return new ResultVO<>();
        }
        return new ResultVO<>(500,"插入失败！");
    }

    @RequestMapping(value = "{id}",method = RequestMethod.GET)
    public ResultVO<Role> selectRole(@PathVariable("id") Integer id){
        Role role = roleService.selectRoleById(id);
        if (role !=null){
            return new ResultVO<>(role);
        }
        return new ResultVO<>(500,"服务器异常！");
    }

    @RequestMapping(value = "",method = RequestMethod.PUT)
    public ResultVO updateRole(Role role,
                               @RequestParam(value = "list", required = false)List<com.kkb.pojo.Resource> list,
                               @RequestParam("list1") List<Integer> list1){
        int i = roleService.updateRole(role, list1);
        if (i>0){
            return new ResultVO();
        }else if (i==0){
            return new ResultVO(200,"未作出更新！");
        }
        return new ResultVO(500,"服务器异常！");
    }

    @RequestMapping(value = "{id}",method = RequestMethod.DELETE)
    public ResultVO logicalDeleteRole(@PathVariable("id") Integer id){
        int i = roleService.logicalDeleteRole(id);
        if (i>0){
            return new ResultVO();
        }
        return new ResultVO(500,"服务器异常");
    }

    @RequestMapping(value = "list",method = RequestMethod.DELETE)
    public ResultVO deleteRoleList(@RequestParam("check") List<Integer> check){
        int i = roleService.deleteRoleList(check);
        if (i>0){
            return new ResultVO();
        }
        return new ResultVO(500,"删除失败，请稍后再试");

    }

    @RequestMapping(value = "all",method = RequestMethod.GET)
    public ResultVO<Role> selectAllRoleList(){
        List<Role> roles = roleService.selectRoleList();
        if (roles!=null){
            return new ResultVO<Role>(roles);
        }
        return new ResultVO<>(404,"暂时无角色信息");
    }
}
